//
//  PayMoneyDetailnfoViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/12.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "PayMoneyDetailnfoViewController.h"

@interface PayMoneyDetailnfoViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableview;
@property (strong, nonatomic) UIButton * btn;

@end

@implementation PayMoneyDetailnfoViewController

static NSString * cellID = @"cell";
static NSString * PayMoneyDetailInfoTableViewCellID = @"PayMoneyDetailInfoTableViewCell";
static NSString * PayMoneyDetailInfoOfUserTableViewCellID = @"PayMoneyDetailInfoOfUserTableViewCell";
static NSString * PayMoneyDetailInfoOtherTableViewCellID = @"PayMoneyDetailInfoOtherTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"缴费账单详情";
    [self creatTableview];
    [self registCell];
    if (self.isPay) {
        [self.view addSubview:self.btn];
    }
}

-(void)registCell
{
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    [self.tableview registerClass:[PayMoneyDetailInfoTableViewCell class] forCellReuseIdentifier:PayMoneyDetailInfoTableViewCellID];
    [self.tableview registerClass:[PayMoneyDetailInfoOfUserTableViewCell class] forCellReuseIdentifier:PayMoneyDetailInfoOfUserTableViewCellID];
    [self.tableview registerClass:[PayMoneyDetailInfoOtherTableViewCell class] forCellReuseIdentifier:PayMoneyDetailInfoOtherTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview;
        if (self.isPay) {
            tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH, WIDTH, HEIGHT - kStatusAndNavBarH - kHomeLineH - 50) style:UITableViewStyleGrouped];
        }else
        {
            tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH, WIDTH, HEIGHT - kStatusAndNavBarH - kHomeLineH) style:UITableViewStyleGrouped];
        }
        tableview.delegate = self;
        tableview.dataSource = self;
        tableview.backgroundColor = SFM242;
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 127;
    }else if (indexPath.section == 1)
    {
        return 157;
    }else
    {
        return 55;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
    line.backgroundColor = SFM242;
    return line;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return 6;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        PayMoneyDetailInfoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PayMoneyDetailInfoTableViewCellID];
        [cell send];
        return cell;
    }else if (indexPath.section == 1)
    {
        PayMoneyDetailInfoOfUserTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PayMoneyDetailInfoOfUserTableViewCellID];
        [cell send];
        return cell;
    }else
    {
        PayMoneyDetailInfoOtherTableViewCell * cell =  [tableView dequeueReusableCellWithIdentifier:PayMoneyDetailInfoOtherTableViewCellID];
        if (indexPath.row == 0) {
            cell.titlelLabel.text = @"定金";
            cell.detailLabel.text = @"10000.00元";
        }else if (indexPath.row == 1)
        {
            cell.titlelLabel.text = @"面积";
            cell.detailLabel.text = @"60㎡";
        }else if (indexPath.row == 2)
        {
            cell.titlelLabel.text = @"水费";
            cell.detailLabel.text = @"100.00元";
        }else if (indexPath.row == 3)
        {
            cell.titlelLabel.text = @"电费";
            cell.detailLabel.text = @"100元";
        }else if (indexPath.row == 4)
        {
            cell.titlelLabel.text = @"物业管理费";
            cell.detailLabel.text = @"200.00元";
        }else
        {
            cell.titlelLabel.text = @"其他费用";
            cell.detailLabel.text = @"800.00元";
        }
        
        return cell;
    }
//    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(UIButton *)btn
{
    if (_btn == nil) {
        _btn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.tableview.frame), WIDTH, 50)];
        [_btn setTitle:@"确认缴费" forState:UIControlStateNormal];
        [_btn setBackgroundColor:SFMNavBarColor];
        _btn.titleLabel.font = font(16);
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}

#pragma mark - 确认缴费
-(void)btnClick
{
    WeakSelf;
    [[PayMedthView shareView] showView];
    [PayMedthView shareView].payMedthChooseTypeEventBlock = ^(int payType) {
        if (payType == payMedthodWayOfAliPay) {
            [weakSelf AliPay];
        }else if(payType == payMedthodWayofWeiChat) {
            [weakSelf weiXinPay];
        }
    };
}

#pragma mark - 支付宝支付
-(void)AliPay{
//    NSMutableDictionary * params = [NSMutableDictionary dictionary];
//    [params setValue:SFMsignKey forKey:@"signKey"];
//    [params setValue:self.money forKey:@"totalAmount"];
//    WeakSelf;
//    [[TNGHTTP shareHttpClient] requestWithPath:@"app/user/alipay" AndParams:params AndReturnBlock:^(NSDictionary *dic) {
//        NSString *appScheme = @"FiveEightLaHuoAPP";
//        NSString * orderString = dic[@"orderString"];
//        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
//            NSString * str =resultDic[@"resultStatus"];
//            NSString * payMessage  = @"支付失败";
//            if ([str isEqualToString:@"6001"]) {
//                payMessage  = @"支付失败";
//            }else if([str isEqualToString:@"6002"])
//            {
//                payMessage  = @"网络延迟,请稍后再试";
//            }else if([str isEqualToString:@"9000"])
//            {
//                payMessage  = @"支付成功";
//                [weakSelf moneyDidChange];
//            }else
//            {
//                payMessage  = @"交易延迟,请稍后再试";
//            }
//            UIAlertController * alert = [UIAlertController alertControllerWithTitle:payMessage message:@"" preferredStyle:UIAlertControllerStyleAlert];
//            UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                PaySucceedViewController * ctr =[[PaySucceedViewController alloc] init];
//                [weakSelf.navigationController pushViewController:ctr animated:YES];
//            }];
//            [alert addAction: action1];
//            [weakSelf presentViewController:alert animated:YES completion:nil];
//            weakSelf.sureBtn.userInteractionEnabled = YES;
//        }];
//    }AndFailMessage:^(NSString * message) {
//        [MBProgressHUD showMessageWithDelay:message delay:2];
//        weakSelf.sureBtn.userInteractionEnabled = YES;
//    }];
}

#pragma mark - 微信支付
-(void)weiXinPay
{
//    int money = [self.money floatValue]*100;
//
//    NSMutableDictionary * params = [NSMutableDictionary dictionary];
//    [params setValue:SFMsignKey forKey:@"signKey"];
//    [params setValue:[NSString stringWithFormat:@"%d",money] forKey:@"txnAmt"];
//
//    WeakSelf;
//    [[TNGHTTP shareHttpClient] requestWithPath:@"app/unionpay/wxApppay" AndParams:params AndReturnBlock:^(NSDictionary *dic) {
//        NSDictionary * dict = dic[@"map"];
//        NSString * partnerId = [NSString stringWithFormat:@"%@",dict[@"mch_id"]];
//        NSString * prepayId  = [NSString stringWithFormat:@"%@",dict[@"prepay_id"]];
//        NSString * nonceStr  = [NSString stringWithFormat:@"%@",dict[@"nonce_str"]];
//        NSString * sign      = [NSString stringWithFormat:@"%@",dict[@"sign"]];
//        NSString * timestamp = [NSString stringWithFormat:@"%@",dict[@"timestamp"]];
//
//        //调起微信支付
//        PayReq * req            = [[PayReq alloc] init];
//        req.partnerId           = partnerId;
//        req.prepayId            = prepayId;
//        req.nonceStr            = nonceStr;
//        req.timeStamp           = [timestamp intValue];
//        req.sign                = sign;
//        req.package             = @"Sign=WXPay";
//        [WXApi sendReq:req];
//        weakSelf.sureBtn.userInteractionEnabled = YES;
//    }AndFailMessage:^(NSString * message) {
//        [MBProgressHUD showMessageWithDelay:message delay:2];
//        weakSelf.sureBtn.userInteractionEnabled = YES;
//    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
